#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
QString A0,A1,B0,B1,C0,C1,D0,D1;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_Left_clicked()
{
    A0 = ui->T1->toPlainText();
    A1 = ui->T2->toPlainText();
    B0 = ui->T3->toPlainText();
    B1 = ui->T4->toPlainText();
    C0 = ui->T5->toPlainText();
    C1 = ui->T6->toPlainText();
    D0 = ui->T7->toPlainText();
    D1 = ui->T8->toPlainText();

    QString total_A,total_B,total_C,total_D,total_A0,total_A1,total_B0,total_B1,total_C0,total_C1,total_D0,total_D1;

    total_A0 = A0.prepend("0");
    total_A1 = A1.prepend("1");
    total_A = total_A0.append("|");
    total_A = total_A.append(total_A1);
    ui->textEdit->insertPlainText(total_A);

    total_B0 = B0.prepend("0");
    total_B1 = B1.prepend("1");
    total_B = total_B0.append("|");
    total_B = total_B.append(total_B1);
    ui->textEdit_2->insertPlainText(total_B);

    total_C0 = C0.prepend("0");
    total_C1 = C1.prepend("1");
    total_C = total_C0.append("|");
    total_C = total_C.append(total_C1);
    ui->textEdit_3->insertPlainText(total_C);

    total_D0 = D0.prepend("0");
    total_D1 = D1.prepend("1");
    total_D = total_D0.append("|");
    total_D = total_D.append(total_D1);
    ui->textEdit_4->insertPlainText(total_D);
}

void MainWindow::on_Right_clicked()
{

    bool f;
    int i;
    QString result_A,result_B,result_C,result_D;
    QString a[8];

   a[1]= A0 = ui->T1->toPlainText();
   a[2]= A1 = ui->T2->toPlainText();
   a[3]= B0 = ui->T3->toPlainText();
   a[4]= B1 = ui->T4->toPlainText();
   a[5]= C0 = ui->T5->toPlainText();
   a[6]= C1 = ui->T6->toPlainText();
   a[7]= D0 = ui->T7->toPlainText();
   a[8]= D1 = ui->T8->toPlainText();


   f=false;
     for(i=1;i<=8;i++){
         if(a[i]=="A"){
             if(f==false){
             if(i==1){
                 result_A="A0";
                 f=true;
                 continue;
             }
             if(i==2){
                 result_A="A1";
                 f=true;
                 continue;
             }
             if(i==3){
                 result_A="B0";
                 f=true;
                 continue;
             }
             if(i==4){
                 result_A="B1";
                 f=true;
                 continue;
             }
             if(i==5){
                 result_A="C0";
                 f=true;
                 continue;
             }
             if(i==6){
                 result_A="C1";
                 f=true;
                 continue;
             }
             if(i==7){
                 result_A ="D0";
                 f=true;
                 continue;
             }
             if(i==8){
                 result_A="D1";
                 f=true;
                 continue;
             }
         }
             if(f==true){
                 if(i==1){
                     result_A = result_A.append("|A0");
                     continue;
                 }
                 if(i==2){
                     result_A = result_A.append("|A1");
                     continue;
                 }
                 if(i==3){
                     result_A = result_A.append("|B0");
                     continue;
                 }
                 if(i==4){
                     result_A = result_A.append("|B1");
                     continue;
                 }
                 if(i==5){
                     result_A = result_A.append("|C0");
                     continue;
                 }
                 if(i==6){
                     result_A = result_A.append("|C1");
                     continue;
                 }
                 if(i==7){
                     result_A = result_A.append("|D0");
                     continue;
                 }
                 if(i==8){
                     result_A = result_A.append("|D1");
                     continue;
                 }
             }
         }
     }

     f=false;
       for(i=1;i<=8;i++){
           if(a[i]=="B"){
               if(f==false){
               if(i==1){
                   result_B="A0";
                   f=true;
                   continue;
               }
               if(i==2){
                   result_B="A1";
                   f=true;
                   continue;
               }
               if(i==3){
                   result_B="B0";
                   f=true;
                   continue;
               }
               if(i==4){
                   result_B="B1";
                   f=true;
                   continue;
               }
               if(i==5){
                   result_B="C0";
                   f=true;
                   continue;
               }
               if(i==6){
                   result_B="C1";
                   f=true;
                   continue;
               }
               if(i==7){
                   result_B="D0";
                   f=true;
                   continue;
               }
               if(i==8){
                   result_B="D1";
                   f=true;
                   continue;
               }
           }
               if(f==true){
                   if(i==1){
                       result_B = result_B.append("|A0");
                       continue;
                   }
                   if(i==2){
                       result_B = result_B.append("|A1");
                       continue;
                   }
                   if(i==3){
                       result_B = result_B.append("|B0");
                       continue;
                   }
                   if(i==4){
                       result_B = result_B.append("|B1");
                       continue;
                   }
                   if(i==5){
                       result_B = result_B.append("|C0");
                       continue;
                   }
                   if(i==6){
                       result_B = result_B.append("|C1");
                       continue;
                   }
                   if(i==7){
                       result_B = result_B.append("|D0");
                       continue;
                   }
                   if(i==8){
                       result_B = result_B.append("|D1");
                       continue;
                   }
               }
           }
       }

       f=false;
         for(i=1;i<=8;i++){
             if(a[i]=="C"){
                 if(f==false){
                 if(i==1){
                     result_C="A0";
                     f=true;
                     continue;
                 }
                 if(i==2){
                     result_C="A1";
                     f=true;
                     continue;
                 }
                 if(i==3){
                     result_C="B0";
                     f=true;
                     continue;
                 }
                 if(i==4){
                     result_C="B1";
                     f=true;
                     continue;
                 }
                 if(i==5){
                     result_C="C0";
                     f=true;
                     continue;
                 }
                 if(i==6){
                     result_C="C1";
                     f=true;
                     continue;
                 }
                 if(i==7){
                     result_C="D0";
                     f=true;
                     continue;
                 }
                 if(i==8){
                     result_C="D1";
                     f=true;
                     continue;
                 }
             }
                 if(f==true){
                     if(i==1){
                         result_C = result_C.append("|A0");
                         continue;
                     }
                     if(i==2){
                         result_C = result_C.append("|A1");
                         continue;
                     }
                     if(i==3){
                         result_C = result_C.append("|B0");
                         continue;
                     }
                     if(i==4){
                         result_C = result_C.append("|B1");
                         continue;
                     }
                     if(i==5){
                         result_C = result_C.append("|C0");
                         continue;
                     }
                     if(i==6){
                         result_C = result_C.append("|C1");
                         continue;
                     }
                     if(i==7){
                         result_C = result_C.append("|D0");
                         continue;
                     }
                     if(i==8){
                         result_C = result_C.append("|D1");
                         continue;
                     }
                 }
             }
         }

         f=false;
           for(i=1;i<=8;i++){
               if(a[i]=="D"){
                   if(f==false){
                   if(i==1){
                       result_D="A0";
                       f=true;
                       continue;
                   }
                   if(i==2){
                       result_D="A1";
                       f=true;
                       continue;
                   }
                   if(i==3){
                       result_D="B0";
                       f=true;
                       continue;
                   }
                   if(i==4){
                       result_D="B1";
                       f=true;
                       continue;
                   }
                   if(i==5){
                       result_D="C0";
                       f=true;
                       continue;
                   }
                   if(i==6){
                       result_D="C1";
                       f=true;
                       continue;
                   }
                   if(i==7){
                       result_D="D0";
                       f=true;
                       continue;
                   }
                   if(i==8){
                       result_D="D1";
                       f=true;
                       continue;
                   }
               }
                   if(f==true){
                       if(i==1){
                           result_D = result_D.append("|A0");
                           continue;
                       }
                       if(i==2){
                           result_D = result_D.append("|A1");
                           continue;
                       }
                       if(i==3){
                           result_D = result_D.append("|B0");
                           continue;
                       }
                       if(i==4){
                           result_D = result_D.append("|B1");
                           continue;
                       }
                       if(i==5){
                           result_D = result_D.append("|C0");
                           continue;
                       }
                       if(i==6){
                           result_D = result_D.append("|C1");
                           continue;
                       }
                       if(i==7){
                           result_D = result_D.append("|D0");
                           continue;
                       }
                       if(i==8){
                           result_D = result_D.append("|D1");
                           continue;
                       }
                   }
               }
           }

  ui->textEdit->insertPlainText(result_A);
  ui->textEdit_2->insertPlainText(result_B);
  ui->textEdit_3->insertPlainText(result_C);
  ui->textEdit_4->insertPlainText(result_D);
}
